package com.imageim.yimirror.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.DailyRecommendBean;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.imageim.yimirror.common.util.ClickUtil;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yuaihen on 2018/4/12.
 * 每日推荐Adapter
 */

public class DailyRecommendAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<DailyRecommendBean.ClothingsBean> mData = new ArrayList<>();
    private OnItemOnclickListener mOnItemOnclickListener;

    public DailyRecommendAdapter(Context context, List<DailyRecommendBean.ClothingsBean> data) {
        this.mContext = context;
        if (data != null) {
            this.mData.addAll(data);
        }
    }

    public void setData(List<DailyRecommendBean.ClothingsBean> list) {
        if (list != null) {
            mData.clear();
            mData.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public DailyRecommendHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_daily_recommend, parent, false);
        return new DailyRecommendHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        DailyRecommendHolder dailyRecommendHolder = ((DailyRecommendHolder) holder);

        DailyRecommendBean.ClothingsBean bean = mData.get(position);
        if (bean == null) {
            return;
        }

        if (!TextUtils.isEmpty(bean.getClothingUrl())) {
            GlideUtils.display(dailyRecommendHolder.mIvModelPhoto, bean.getClothingUrl());
        }

        //设置适用场合
        String occasionNames = bean.getOccasionNames();
        if (!TextUtils.isEmpty(occasionNames)) {
            dailyRecommendHolder.mTvSuitOccasion.setVisibility(View.VISIBLE);
            String[] suitOccasionNames = occasionNames.split(",");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < suitOccasionNames.length; i++) {
                stringBuilder.append(suitOccasionNames[i]);
                if (i != suitOccasionNames.length - 1) {
                    stringBuilder.append("、");
                }
            }
            dailyRecommendHolder.mTvSuitOccasion.setText(MessageFormat.format("适合场合：{0}", stringBuilder.toString()));
        } else {
            dailyRecommendHolder.mTvSuitOccasion.setVisibility(View.GONE);
        }

        //已收藏
        if (bean.getCollectFlag() == 1) {
            dailyRecommendHolder.mIvLike.setSelected(true);
            dailyRecommendHolder.mTvLike.setSelected(true);
            dailyRecommendHolder.mTvLike.setText("取消");
        } else {
            dailyRecommendHolder.mIvLike.setSelected(false);
            dailyRecommendHolder.mTvLike.setSelected(false);
            dailyRecommendHolder.mTvLike.setText("喜欢");
        }

        if (mOnItemOnclickListener != null) {
            dailyRecommendHolder.llShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemOnclickListener.onClickShare(position);
                }
            });

            dailyRecommendHolder.llLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ClickUtil.isValidClick()) {
                        if (bean.getCollectFlag() == 1) {
                            //取消收藏
                            dailyRecommendHolder.mIvLike.setSelected(false);
                            dailyRecommendHolder.mTvLike.setSelected(false);
                            dailyRecommendHolder.mTvLike.setText("喜欢");
                            bean.setCollectFlag(0);
                            mOnItemOnclickListener.onClickLike(position, false);
                        } else {
                            //添加收藏
                            dailyRecommendHolder.mIvLike.setSelected(true);
                            dailyRecommendHolder.mTvLike.setSelected(true);
                            dailyRecommendHolder.mTvLike.setText("取消");
                            bean.setCollectFlag(1);
                            mOnItemOnclickListener.onClickLike(position, true);
                        }
                    }
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    class DailyRecommendHolder extends RecyclerView.ViewHolder {
        private ImageView mIvModelPhoto, mIvLike;
        private LinearLayout llShare, llLike;
        private TextView mTvLike, mTvSuitOccasion;

        public DailyRecommendHolder(View itemView) {
            super(itemView);
            mIvModelPhoto = itemView.findViewById(R.id.iv_modelPhoto);
            llShare = itemView.findViewById(R.id.ll_share);
            llLike = itemView.findViewById(R.id.ll_like);
            mIvLike = itemView.findViewById(R.id.iv_like);
            mTvLike = itemView.findViewById(R.id.tv_like);
            mTvSuitOccasion = itemView.findViewById(R.id.tv_suit_occasion);
        }
    }

    public interface OnItemOnclickListener {
        void onClickShare(int position);

        void onClickLike(int position, boolean collect);
    }

    public void setOnItemOnclickListener(OnItemOnclickListener onItemOnclickListener) {
        mOnItemOnclickListener = onItemOnclickListener;
    }
}